﻿using Common;
using NLog;
using System;
using System.Windows.Forms;
using TempCtr.Forms;
using TempCtr.Model;

namespace TempCtr
{
    public partial class MainFrom : Form
    {
        public static readonly Logger Logger = LogManager.GetCurrentClassLogger();
        public static bool isUpdate = false;
        public MainFrom()
        {
            InitializeComponent();
        }
        private string encryptComputer = string.Empty;
        public static string md5key = string.Empty;
        public static bool isRegist = false;
        private void MainFrom_Load(object sender, EventArgs e)
        {
           
            string computer = ComputerInfo.GetComputerInfo();
            encryptComputer = new EncryptionHelper().EncryptString(computer);
            md5key = new EncryptionHelper().GetMD5String(encryptComputer);

            if (RegistFileHelper.CheckRegistData(md5key) == true)
            {
                lbRegistInfo.Visible = false;
                isRegist = true;
            }
            else
            {
                lbRegistInfo.Text = "待注册，运行十分钟后自动关闭";
                timer1.Start();
            }

            Logger.Info("***********软件打开*************");
            this.WindowState = FormWindowState.Maximized;
            Thermostat.Init();
            
            AddControls();
          
            if (Thermostat.IsOpen1 == true) pictureBox1.Image = Properties.Resources.green;
            else pictureBox1.Image = Properties.Resources.gray;
            ThermostatSerialPort.Cmd_Operation(Thermostat.SP1, 1, 1, 3, 26, 2, 2000);
            if (Thermostat.IsOpen2 == true) pictureBox2.Image = Properties.Resources.green;
            else pictureBox2.Image = Properties.Resources.gray;
            if (Thermostat.IsOpen3 == true) pictureBox3.Image = Properties.Resources.green;
            else pictureBox3.Image = Properties.Resources.gray;
            if (Thermostat.IsOpen4 == true) pictureBox4.Image = Properties.Resources.green;
            else pictureBox4.Image = Properties.Resources.gray;
            if (Thermostat.IsOpen5 == true) pictureBox5.Image = Properties.Resources.green;
            else pictureBox5.Image = Properties.Resources.gray;
            if (Thermostat.IsOpen6 == true) pictureBox6.Image = Properties.Resources.green;
            else pictureBox6.Image = Properties.Resources.gray;
            if (Thermostat.IsOpen7 == true) pictureBox7.Image = Properties.Resources.green;
            else pictureBox7.Image = Properties.Resources.gray;
            if (Thermostat.IsOpen8 == true) pictureBox8.Image = Properties.Resources.green;
            else pictureBox8.Image = Properties.Resources.gray;
            if (Thermostat.IsOpen9 == true) pictureBox9.Image = Properties.Resources.green;
            else pictureBox9.Image = Properties.Resources.gray;
            if (Thermostat.IsOpen10 == true) pictureBox10.Image = Properties.Resources.green;
            else pictureBox10.Image = Properties.Resources.gray;

            Thermostat.FirstQuery();
        }

        private void AddControls()
        {
            try
            {
                fLP_CardList.Controls.Clear();
                foreach (var Th in Thermostat.ThermostatDic)
                {
                    fLP_CardList.Controls.Add(Th.Value.Card);
                }
                Logger.Info("***********控件加载完成*************");
            }
            catch (Exception ex)
            {
                Logger.Error("加载控件错误"+ex.ToString());
            }
                 
        }

        private void MainFrom_FormClosing(object sender, FormClosingEventArgs e)
        {
           // Application.Exit();
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (isUpdate == true || Thermostat.ThermostatDic.Keys.Count == 0)
                {
                    Thermostat.Init();
                    AddControls();
                    MessageBox.Show("刷新成功！");
                    isUpdate = false;
                }
                else
                {
                    MessageBox.Show("刷新成功！");
                }
            }
            catch
            {

            }
           
            
        }

        private void 温控串口ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            port_setting f1 = new port_setting();
            f1.ShowDialog();
        }

        private void 短信ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("未连接短信猫！");
           
        }

        private void 温控器设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            equip_manage f1 = new equip_manage();
            f1.ShowDialog();
        }

        private void 测试ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            log_query f1 = new log_query();
            f1.ShowDialog();
        }

        private void MainFrom_Resize(object sender, EventArgs e)
        {
            fLP_CardList.Width = this.Width - 28;
            fLP_CardList.Height = this.Height - 180;
        }
        int count = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            count++;
            if (count > 300 && isRegist == false)
            {
                Logger.Info("************试运行结束*************");
                this.Close();//在其他界面时，会报错
            }
            if (isRegist == true)
            {
                count = 0;
                timer1.Enabled = false;
                lbRegistInfo.Visible = false;        
                return;
            }
        }

        private void 注册ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (isRegist)
            {
                MessageBox.Show("已注册！");
            }
            else
            {
                RegistForm registform = new RegistForm();
                Logger.Info("***********点击注册*************");
                registform.ShowDialog();
            }
        }

        private void 温度设定ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setForm setform = new setForm();
            setform.ShowDialog();          
        }


    }
}
